package com.hurt.competition.common.handler;

import com.hurt.competition.common.contans.CoreContants;
import com.hurt.competition.common.exception.CoreException;
import com.hurt.competition.common.exception.LoginException;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(CoreException.class)
    public CoreResult handlerCore(Exception e){
        return CoreResultBuilder.error(e.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public CoreResult handlerLogin(Exception e){
        return CoreResultBuilder.error(e.getMessage());
    }

    @ExceptionHandler(value = {Exception.class,RuntimeException.class})
    public CoreResult handlerRuntime(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        return CoreResultBuilder.error(CoreContants.ERROR_EXCEPTION);
    }
}
